package utils.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import utils.Logger;

public class Config {
	
	private static final String CFILE = "config/config.txt";
	// singleton
	private static Config conf = null;
	
	public static Config getInstance() {
		if (conf == null) {
			conf = new Config();
		}
		return conf;
	}
	
	private Config() {
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(CFILE)));
			String line;
			while((line = br.readLine()) != null) {
				line = line.trim();
				if(line.length()==0) continue;
				if(line.startsWith("//")) continue;
				String s[] = line.split("=");
				parse(s[0].trim(),s[1].trim());
			}
		} catch(Exception e) {
			System.err.println("Cannot read config file!");
			e.printStackTrace();
		}
	}
	
	
	
	public String network_map_file = null;
	public String network_map_bin = null;
	public String pls_folder = null;
	public Calendar pls_start_time = null;
	public Calendar	pls_end_time = null;
	public String users_csv_folder = null;
	
	private void parse(String name, String value) {
		if(name.equals("network_map_file")) {
			network_map_file = value;
		}
		else if(name.equals("network_map_bin")) {
			network_map_bin = value;
		}
		else if(name.equals("pls_folder")) {
			pls_folder = value;
		}
		else if(name.equals("pls_start_time")) {
			SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
			pls_start_time = new GregorianCalendar();
			try {
				pls_start_time.setTime(f.parse(value));
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		else if(name.equals("pls_end_time")) {
			SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy HH:mm");
			pls_end_time = new GregorianCalendar();
			try {
				pls_end_time.setTime(f.parse(value));
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		else if(name.equals("users_csv_folder")) {
			users_csv_folder = value;
			new File(users_csv_folder).mkdir();
		}
		else {
			Logger.logln("Warning: parameter "+name+" = "+value+" unrecognized!");
		}
	}
}
